package org.example.lease.common.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
public class Result <T>{
    /*返回码*/
    @Schema(description = "返回码")
    private Integer code;
    /*返回消息*/
    @Schema(description = "返回消息")
    private String message;
    /*返回数据*/
    @Schema(description = "返回数据")
    private T Data;

    public Result(){}


    private static <T> Result<T> build(T data){
        Result<T> result = new Result<>();
        if(data!=null)
            result.setData(data);
        return result;
    }
    public static <T> Result<T> build(T body,ResultCodeEnum resultCodeEnum){
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }
    public static <T> Result<T> ok(T data){
        return build(data,ResultCodeEnum.SUCCESS);
    }
    public static <T> Result ok(){
        return Result.ok(null);
    }
    public static <T> Result<T> fail(){
        return build(null,ResultCodeEnum.FAIL);
    }

    public static <T> Result<T> fail(Integer code,String message){
        Result<T> result=build(null);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
}
